<template>
  <div class="mt-form"><slot></slot></div>
</template>

<script>
  const { schema } = require('../../utils/compile/validator')
  const objectPath = require('../../static/object-path')
  const formItemBehavior = require('../../static/form-item.behavior')

  export default {
    relations: {
      formItemBehavior: {
        type: 'descendant', // 关联的目标节点应为子孙节点,
        target: formItemBehavior,
        linked: function (formItem) {
          // console.log('formItems', this.formItems, formItem)
          this.formItems[formItem.$id] = formItem
        },
        unlinked: function (formItem) {
          delete this.formItems[formItem.$id]
        }
      }
    },
    properties: {
      value: null,
    },
    observers: {
      'value': function (field) {
        // console.log('field', field, this.data.value)
        this.formValue = this.data.value
      }
    },
    created() {
      // console.log('created')
      this.formValue = null
      this.formItems = null
    },
    attached() {
      this.formItems = {}
    },
    data() {
      return {
        descriptor: {}
      }
    },
    methods: {
      setDescriptor(d) {
        this.descriptor = d
      },
      validate(callback) {
        let validator = new schema(this.descriptor)
        validator.validate(this.formValue, callback)
      },
      validateField(props, cb) {

      },
      mtFormChange(prop, val) {
        console.log('mtFormChange', prop, val)
        console.log('formValue', this.formValue, prop, val)
        objectPath.set(this.formValue, prop, val)
      },
      getValue() {
        return this.formValue
      }
    }
  }
</script>
